  /**************************************************************************/
  /*                                                                        */
  /*  System  Name :  ICIM                                                  */
  /*                                                                        */
  /*  Description  :  Product Management                                    */
  /*                                                                        */
  /*  MODIFICATION HISTORY                                                  */
  /*    Date     Ver     Name          Description                          */
  /* ---------- ----- ----------- ----------------------------------------- */
  /* 2017/09/06 N0.00                     Initial release                   */
  /*                                                                        */
  /**************************************************************************/

$(document).ready(function() {
	$("form").submit(function() {
		return false;
	});
	var YNBean={
		Value : ['Y','N'],
		Text:{
			Y : 'Y:YES',
			N : 'N:NO'
		}
	};
	var routeBean = CreateRouteBean();
	var inObj_Query;
	var PathIdAndVer = {};
	var VAL = {
		T_FBPBISBOM : "FBPBISBOM",
		T_FBPBMDLDF : "FBPMDLDEF",
		T_FBPBISOPE : "FBPBISOPE",
		T_FBPBISMTR : "FBPBISMTR",
		T_FBPBISPTH : "FBPBISPTH",
		T_FBPBISLAY : "FBPBISLAY",
		T_FBPBISCUS : "FBPBISCUS",
		NORMAL : "0000000",
		DISABLED_ATTR : {
    		"disabled" : true
		},
    	ENABLED_ATTR : {
    		"disabled" : false
		},
		DISABLED_FADETO :{
			"fadeTo"   : 0.25
		},
		ENABLED_FADETO : {
    		"fadeTo" : 1.0
		}
	};
	var domObj = {
		$mdlIdTxt : $("#mdlIdTxt"),
		$layoutIDSel : $("#layoutIDSel"),
		$mdlCateSel : $("#mdlCateSel"),
		$mdlSizeTxt : $("#mdlSizeTxt"),
		$cusIDSel   : $("#cusIDSel"),
		$cusMdlIdTxt: $("#cusMdlIdTxt"),
		$maxPFCntTxt: $("#maxPFCntTxt"),
		$SWHtimeTxt : $("#SWHtimeTxt"),
		$validFlgSel: $("#validFlgSel"),
		$mdlDscTxt : $("#mdlDscTxt"),
		$okRadioTxt : $("#okRadioTxt"),
		$boxCntTxt : $("#boxCntTxt"),
		$fromThicknessTxt : $("#fromThicknessTxt"),
		$toThicknessTxt : $("#toThicknessTxt"),
		$pathIdSel : $("#pathIdSel"),
		$pathVerSel :$("#pathVerSel"),
        $lotFlgSel : $("#lotFlgSel"),
        $maxRwCntTxt : $("#maxRwCntTxt"),
		buttons : {
			$query_btn 	: $("#query_btn"),
	        $delete_btn 	: $("#delete_btn"),
	        $add_btn     : $("#add_btn"),
	        $update_btn  : $("#update_btn"),
	        $copy_btn    : $("#copy_btn"),
	        $save_btn : $("#save_btn"),
	        $rollback_btn : $("#rollback_btn"),
	        $clear_btn : $("#clear_btn"),
			$addBomMtrl : $("#add_bom_btn"),
			$updateBomMtrl : $("#update_bom_btn"),
			$deleteBomMtrl : $("#del_bom_btn"),
			$add_level_btn : $("#add_level_btn"),
			$update_level_btn : $("#update_level_btn"),
			$del_level_btn : $("#del_level_btn")
			
		},
		grids : {
			$mdlDefListGrd : $("#mdlDefListGrd"),
			$bomListGrd : $("#bomListGrd"),
			$levelListGrd : $("#levelListGrd")
		},
		dialogs : {
			$queryMdlDefDialog : $("#queryMdlDefDialog"),
			$queryMdlDefDialog_queryBtn : $("#queryMdlDefDialog_queryBtn"),
			$queryMdlDefDialog_mdlIdTxt : $("#queryMdlDefDialog_mdlIdTxt"),
			$bomEditDialog : $("#bomEditDialog"),
			$bomEditDialog_mdlIdTxt : $("#bomEditDialog_mdlIdTxt"),
			$bomEditDialog_mdlMtrlSeqIDTxt : $("#bomEditDialog_mdlMtrlSeqIDTxt"),
			$bomEditDialog_OpeIDSel : $("#bomEditDialog_OpeIDSel"),
			$bomEditDialog_mtrlProdIDSel : $("#bomEditDialog_mtrlProdIDSel"),
			$bomEditDialog_mtrlCatesel : $("#bomEditDialog_mtrlCatesel"),
			$bomEditDialog_mtrlUseQtyTxt : $("#bomEditDialog_mtrlUseQtyTxt"),
			$bomEditDialog_mtrlMkrTxt : $("#bomEditDialog_mtrlMkrTxt"),
			
			$levelEditDialog : $("#levelEditDialog"),
			$levelEditDialog_upTxt : $("#levelEditDialog_upTxt"),
			$levelEditDialog_downTxt : $("#levelEditDialog_downTxt"),
			$levelEditDialog_Txt : $("#levelEditDialog_Txt"),
			$levelEditDialog_saveBtn : $("#levelEditDialog_saveBtn")
		}
	};
	var baseFnc = {
		isDisabled : function($selector) {
			return ($selector.attr("disabled") === "disabled" ? true : false);
		}
	};
	var controlsFunc = {
		initLayoutSel : function() {
			var inObj, outObj,tblCnt,i,oary,val,txt;
			SelectDom.initWithSpace(domObj.$layoutIDSel);
			inObj = {
				trx_id : VAL.T_FBPBISLAY,
				action_flg : 'L'
			};
			outObj = comTrxSubSendPostJson(inObj);
			tblCnt = outObj.tbl_cnt ;
			if (outObj.rtn_code === VAL.NORMAL && tblCnt > 0) {
				for(i=0;i<tblCnt;i++){
					txt = outObj.oary[i].layot_id ;
					val = outObj.oary[i].layot_id;
					SelectDom.addSelect(domObj.$layoutIDSel,val,txt);
				}
			}
		},
		initPathIdSel : function() {
			var inObj, outObj,tblCnt,i,oary,val,txt;
			domObj.$pathIdSel.empty();
			inObj = {
				trx_id : VAL.T_FBPBISPTH,
				action_flg : "Q",
				iaryA : [{
					path_cate : "MAN" //MAIN
				}]
			};
			outObj = comTrxSubSendPostJson(inObj);
        	routeBean.initFnc(outObj.oaryA,"path_id","path_ver","path_dsc");
			SelectDom.addSelectArr(
					domObj.$pathIdSel,
					outObj.oaryA, "path_id","path_id","",true);
		},
		pathIdChangeFunc:function(){
			var routeVerObj = routeBean.getVer(domObj.$pathIdSel.val());
			domObj.$pathVerSel.empty();
			SelectDom.addSelectArr(domObj.$pathVerSel, routeVerObj, "ver","ver");
		},
		initMdlCateSel : function() {
			comAddValueByDataCateFnc("#mdlCateSel","MDCT","data_ext","data_desc",true);
		},
		initValidFlag : function(){
			SelectDom.SelectBean(domObj.$validFlgSel,YNBean.Value,YNBean.Text,false);
		},
        initPostFlag : function(){
//            comAddValueByDataCateFnc("#lotFlgSel", "PSFG", "data_ext","data_desc",true);
            $("#lotFlgSel").empty();
            $("#lotFlgSel").append("<option value = 'S'>S：单片入账</option>");
            $("#lotFlgSel").append("<option value = 'L'>L：批次入账</option>");
            $("#lotFlgSel").select2({
    	    	theme : "bootstrap"
    	    });
		},
		initCusID : function(){
			SelectDom.initWithSpace(domObj.$cusIDSel);
			var inObj, outObj;
			var iary = {cus_typ : 'S'};

			inObj = {
					trx_id      : VAL.T_FBPBISCUS,
					action_flg  : 'Q',
						iary	: [iary]
					};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code == VAL.NORMAL) {
				SelectDom.addSelectArr(domObj.$cusIDSel, outObj.oary,"cus_id", "cus_name","",true);
			}
		},
		com_get_1st_inf : function(){
			var rowIds = domObj.grids.$mdlDefListGrd.jqGrid('getDataIDs');
			if(rowIds.length>0){
				domObj.grids.$mdlDefListGrd.setSelection(rowIds[0], true);
			}
		},
		mdlGridSelRowFnc : function(rowId) {
			if(domObj.$mdlDscTxt.attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
				return false;
			}
			var rowData, mdlId, iary, inObj, outObj, oary, i, tblCnt, oaryArr;
			$("input").attr(VAL.DISABLED_ATTR);
			$("select").attr(VAL.DISABLED_ATTR);
			
			rowData = domObj.grids.$mdlDefListGrd.jqGrid("getRowData", rowId);

			mdl_id = rowData.mdl_id;

			inObj = {
				trx_id : VAL.T_FBPBMDLDF,
				action_flg : "Q",
				iary : [{
					mdl_id : mdl_id
				}]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code == VAL.NORMAL) {
				oary = outObj.oary[0];
				if(!oary){
					showErrorDialog("", "该产品代码不存在，请确认！");
					return false;
				}

				domObj.$mdlIdTxt.val($.trim(oary.mdl_id));
				
				SelectDom.setSelect(domObj.$mdlCateSel, $.trim(oary.mdl_cate));

				SelectDom.setSelect(domObj.$pathIdSel, oary.path_id_fk);
				SelectDom.setSelect(domObj.$pathVerSel, oary.path_ver_fk);
				PathIdAndVer.pathId = oary.path_id_fk;
                PathIdAndVer.pathVer = oary.path_ver_fk;
				SelectDom.setSelect(domObj.$validFlgSel,oary.valid_flg);
                SelectDom.setSelect(domObj.$lotFlgSel,oary.post_flag);

				domObj.$boxCntTxt.val(oary.box_cnt);
				domObj.$mdlDscTxt.val($.trim(oary.mdl_dsc));
				domObj.$mdlSizeTxt.val($.trim(oary.mdl_size));
				domObj.$cusMdlIdTxt.val(oary.cus_mdl_id);
				domObj.$okRadioTxt.val(oary.ok_ratio);
				domObj.$fromThicknessTxt.val(oary.from_thickness);
				domObj.$toThicknessTxt.val(oary.to_thickness);
				domObj.$maxPFCntTxt.val(oary.max_pf_cnt);
				domObj.$SWHtimeTxt.val(oary.swh_time);
                domObj.$maxRwCntTxt.val(oary.box_cnt);
				setGridInfo(outObj.oary[0].oaryB, "#levelListGrd");
				inObj = {
					trx_id : VAL.T_FBPBISBOM,
					action_flg : "Q",
					iary : [{
						mdl_id_fk : mdl_id
					}]
				};
				outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code === VAL.NORMAL) {
					setGridInfo(outObj.oary, "#bomListGrd");
				}
			}
		},
		initMdlGirdInfo : function() {
			var itemInfoCM = [{
				name : 'mdl_id',
				index : 'mdl_id',
				label : MDL_ID_TAG,
				width : 170
			}, {
				name : 'mdl_dsc',
				index : 'mdl_dsc',
				label : MDL_DSC_TAG,
				width : 210
			}];
			domObj.grids.$mdlDefListGrd.jqGrid({
				datatype : "local",
	    		autoheight : true,
	    		mtype : "POST",
	    		height : 450,
	    		autowidth : true,//宽度根据父元素自适应
                shrinkToFit:false,
                autoScroll: false,
	    		scroll : true,
	    		resizable : true,
	    		rownumbers : true,
	    		loadonce : true,
	            viewrecords : true,
				colModel : itemInfoCM,
				pager : "mdlDefListPg",
				gridComplete : function() {
					var gridPager, pageLen;
					gridPager = $(this).jqGrid("getGridParam", "pager");
					if (gridPager.length < 2) {
						return false;
					}
					$("#sp_1_" + gridPager.substr(1, pageLen - 1))
							.hide();
					$(".ui-pg-input").hide();
					$('td[dir="ltr"]').hide();
					$(gridPager + "_left").hide();
					$(gridPager + "_center").css({
								width : 0
							});
				},
				onSelectRow : function(id) {
					controlsFunc.mdlGridSelRowFnc(id);
				}
			})
		},
		initBomGridInfo : function() {
			var bomItemInfoCM = [{
				name : 'mdl_id_fk',
				index : 'mdl_id_fk',
				label : MDL_ID_TAG,
				width : 80,
				hidden : true
			}, {
				name : 'mdl_mtrl_seq_id',
				index : 'mdl_mtrl_seq_id',
				label : "SEQ_ID",
				width : 10,
				hidden : true
			}, {
				name : 'mtrl_prod_id_fk',
				index : 'mtrl_prod_id_fk',
				label : MTRL_PRODUCT_ID_TAG,
				width : 120
			}, {
				name : 'mtrl_cate',
				index : 'mtrl_cate',
				label : MTRL_CATE_TAG,
				width : 100
			}, {
				name : 'ope_id_fk',
				index : 'ope_id_fk',
				label : OPE_ID_TAG,
				width : 100
			}, {
				name : 'mtrl_use_qty',
				index : 'mtrl_use_qty',
				label : MTRL_USE_QTY_TAG,
				width : 100
			}];

			domObj.grids.$bomListGrd.jqGrid({
				datatype : "local",
	    		autoheight : true,
	    		mtype : "POST",
	    		height : 350,
	    		autowidth : true,//宽度根据父元素自适应
	    		shrinkToFit : false,
	    		scroll : true,
	    		resizable : true,
	    		rownumbers : true,
	    		loadonce : true,
	            viewrecords : true,
				colModel : bomItemInfoCM,
				gridComplete : function() {
				}
			})
		},
		
		initlevelGridInfo : function() {
			var levelItemInfoCM = [{
				name : 'evt_seq_id',
				index : 'evt_seq_id',
				label : '主键',
				width : 120,
				hidden: true
			},{
				name : 'prd_grade',
				index : 'prd_grade',
				label : '等级',
				width : 120
			},{
				name : 'low_ng_limt',
				index : 'low_ng_limt',
				label : '下限',
				width : 100,
			}, {
				name : 'up_ng_limt',
				index : 'up_ng_limt',
				label : '上限',
				width : 100,
			}, {
				name : 'mdl_id_fk',
				index : 'mdl_id_fk',
				label : '产品名称',
				width : 120,
				hidden : true
			}];
			
			
			domObj.grids.$levelListGrd.jqGrid({
				datatype : "local",
	    		mtype : "POST",
	    		autowidth : true,//宽度根据父元素自适应
	    		shrinkToFit : true,
	    		scroll : true,
	    		resizable : true,
	    		rownumbers : true,
	    		loadonce : true,
	            viewrecords : true,
				sortable : true,
				colModel : levelItemInfoCM
			})
		},

		//check wo
		checkExistWoId : function () {
            var inObj, outObj,tblCnt,i,oary,val,txt;
            //获得该产品当前的工艺路线
			if(typeof(PathIdAndVer.pathId) !== "undefined" && typeof(PathIdAndVer.pathVer) !== "undefined"){
				if(domObj.$pathIdSel.attr("disabled") !== "disabled"){//修改状态
                   /* var pathIdFk = PathIdAndVer.pathId;
                    var pathVerFk = PathIdAndVer.pathVer;
                    var  iary = {
                        path_id_fk : pathIdFk,
                        path_ver_fk : pathVerFk
                    }
                    inObj = {
                        trx_id : VAL.T_FBPBMDLDF,
                        action_flg : "P",
                        iary : [iary]
                    };
                    outObj = comTrxSubSendPostJson(inObj);
                    if(outObj.rtn_code === VAL.NORMAL){
                        return true;
                    }else{
                    	//设回原来的
                        domObj.$pathIdSel.val(PathIdAndVer.pathId);
                        domObj.$pathVerSel.val(PathIdAndVer.pathVer)
					}*/
                    showWarnningDialog({errMsg : "请检查当前工艺路线["+ PathIdAndVer.pathId+"]是否有未结掉的工单！"});
				}
			}

        }
	};
	
	var dialogFnc = {
		queryDialog : {
			queryFnc : function() {
				//outObj是查询回来的对象 ， mdlId 是查询的内容,iary是一个数组对象
				var outObj, mdlId, iary = {};
				mdlId = domObj.dialogs.$queryMdlDefDialog_mdlIdTxt.val();
				if (mdlId) {
					iary.mdl_id = mdlId;
				}
				inObj_Query = {
					trx_id : VAL.T_FBPBMDLDF,
					action_flg : 'Q',
					iary : [iary]
				};
				outObj = comTrxSubSendPostJson(inObj_Query);
				//返回正常，
				if (outObj.rtn_code === VAL.NORMAL) {
					setGridInfo(outObj.oary, "#mdlDefListGrd");
					domObj.dialogs.$queryMdlDefDialog.modal("hide");
					controlsFunc.com_get_1st_inf();
				}
			}
		},
		
		levelDialog : {
			leveladdFnc : function (){
				if(domObj.$mdlDscTxt.attr("disabled") == "disabled"){
					showErrorDialog("", "目前不是编辑状态，不可新增产品等级。");
					return false;
				}
				
				$('#levelEditDialog').modal({
					backdrop : true,
					keyboard : false,
					show : false
				});
				
				$('#levelEditDialog').unbind('shown.bs.modal');
				$("#levelEditDialog_saveBtn").unbind('click');
				$('#levelEditDialog').bind('shown.bs.modal');
				$('#levelEditDialog').modal("show");
				//模态框查询按钮绑定查询函数
				$("#levelEditDialog_saveBtn").bind('click', dialogFnc.levelDialog.addOrUpdatelevelFnc);
				$("#levelEditDialog input").attr(VAL.ENABLED_ATTR);
				$('#levelEditDialog input').val("");
			},
			
			addOrUpdatelevelFnc : function (){
				var  inObj, outObj, actionFlg,oaryB, tblCnt,newRowId,selRowId,rowData;
				actionFlg = domObj.dialogs.$levelEditDialog_Txt.attr("disabled") === "disabled" ? "U" : "A";
				var iaryB = {};
				var  upNgLimit = $.trim(domObj.dialogs.$levelEditDialog_upTxt.val());
				var  lowNgLimt = $.trim(domObj.dialogs.$levelEditDialog_downTxt.val());
				var prdGrade = $.trim(domObj.dialogs.$levelEditDialog_Txt.val());
				if(prdGrade ==""){
					showErrorDialog("", "产品等级不能为空!");
					return false;
				}
				
				if(actionFlg =="A"){
					//得到表格中所有的prdGrade，判重
					var rowDatas = domObj.grids.$levelListGrd.getRowData();
					for(var i = 0 ;i < rowDatas.length; i++){
						if(prdGrade == rowDatas[i].prd_grade){
							showErrorDialog("", "产品等级已重复请重新输入！");
							return false;
						}
					}
					newRowId = getGridNewRowID(domObj.grids.$levelListGrd);
					domObj.grids.$levelListGrd.addRowData(newRowId,{'prd_grade':prdGrade,'low_ng_limt':lowNgLimt,'up_ng_limt':upNgLimit});
					//默认选择新加的行    
					domObj.grids.$levelListGrd.setSelection(newRowId, true); 
					$('#levelEditDialog').modal("hide");
					showSuccessDialog("新增产品等级成功,点击保存确认！");
				}else if (actionFlg =="U"){
					//更新修改行的数据
					selRowId = domObj.grids.$levelListGrd.jqGrid("getGridParam","selrow");
					domObj.grids.$levelListGrd.setRowData(selRowId,{'prd_grade':prdGrade,'low_ng_limt':lowNgLimt,'up_ng_limt':upNgLimit});
					$('#levelEditDialog').modal("hide");
					showSuccessDialog("修改产品等级成功,点击保存确认！");
				}
			},
			
			
			levelupdateFnc : function (){
				if(domObj.$mdlDscTxt.attr("disabled") == "disabled"){
					showErrorDialog("", "目前不是编辑状态，不可更新等级信息。");
					return false;
				}
				var selRowId = domObj.grids.$levelListGrd.jqGrid("getGridParam","selrow");
				if(selRowId == null){
					showErrorDialog("", "请选择需要修改的产品等级");
					return false;
				}
				var rowData = domObj.grids.$levelListGrd.jqGrid("getRowData",selRowId);
				$('#levelEditDialog').modal({
					backdrop : true,
					keyboard : false,
					show : false
				});
				
				$('#levelEditDialog').unbind('shown.bs.modal');
				$("#levelEditDialog_saveBtn").unbind('click');
				$('#levelEditDialog').bind('shown.bs.modal');
				$('#levelEditDialog').modal("show");
				//模态框查询按钮绑定查询函数
				$("#levelEditDialog_saveBtn").bind('click', dialogFnc.levelDialog.addOrUpdatelevelFnc);
				$("#levelEditDialog input").attr(VAL.ENABLED_ATTR);
				$('#levelEditDialog input').val("");
				$("#levelEditDialog_Txt").attr(VAL.DISABLED_ATTR);
				$("#levelEditDialog_Txt").val(rowData.prd_grade);
				$("#levelEditDialog_upTxt").val(rowData.up_ng_limt);
				$("#levelEditDialog_downTxt").val(rowData.low_ng_limt);
			},
			
			leveldeleteFnc : function (){
				if(domObj.$mdlDscTxt.attr("disabled") == "disabled"){
					showErrorDialog("", "目前不是编辑状态，不可删除等级信息。");
					return false;
				}
				var selRowId = domObj.grids.$levelListGrd.jqGrid("getGridParam","selrow");
				if(!selRowId){
					showErrorDialog("", "请选择需要删除的产品等级");
					return false;
				}
				domObj.buttons.$del_level_btn.showCallBackWarnningDialog({
					errMsg : "是否删除物料信息,请确认!!!!",
					callbackFn : function(data) {
						if (data.result == true) {
							domObj.grids.$levelListGrd.jqGrid("delRowData", selRowId);
						}
					}
				})
			}
		},
		
		bomDialog : {
			bomEditDialogMtrlCateSelFnc : function() {
				var iary, inObj, outObj;
				iary = {
					mtrl_cate : $("#bomEditDialog_mtrlCatesel").val()
				};
				inObj = {
					trx_id : VAL.T_FBPBISMTR,
					action_flg : 'Q',
					iary : [iary]
				};
				outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code === VAL.NORMAL ) {
					$("#bomEditDialog_mtrlProdIDSel").empty();
					SelectDom.addSelectArr($("#bomEditDialog_mtrlProdIDSel"),
							outObj.oary, "mtrl_prod_id","mtrl_prod_id","",true)
				}
			},

			addBom : function() {
				if(domObj.$mdlDscTxt.attr("disabled") == "disabled"){
					showErrorDialog("", "目前不是编辑状态，不可新增Bom信息。");
					return false;
				}
				dialogFnc.bomDialog.showBomDialog("A");
			},
			updateBom : function() {
				if(domObj.$mdlDscTxt.attr("disabled") == "disabled"){
					showErrorDialog("", "目前不是编辑状态，不可修改Bom信息。");
					return false;
				}
				var selRowId, rowData;
				selRowId = domObj.grids.$bomListGrd.jqGrid("getGridParam",
						"selrow");
				if (!selRowId) {
					showErrorDialog("", "请选择需要修改的物料信息");
					return false;
				}
				rowData = domObj.grids.$bomListGrd.jqGrid("getRowData",
						selRowId);
				dialogFnc.bomDialog.showBomDialog("U", rowData);
			},
			showBomDialog : function(actionFlg, bomRowData) {
				var inObj, outObj, tblCnt, mdlId;
				mdlId = $.trim(domObj.$mdlIdTxt.val());
				if (!mdlId) {
					showErrorDialog("", "生产型号为空,不能添加物料");
					return false;
				}
//				if (!baseFnc.isDisabled(domObj.$mdlIdTxt)) {
//					showErrorDialog("", "新增物料信息前,请先保存产品信息");
//					return false;
//				}
				domObj.dialogs.$bomEditDialog.modal({
							backdrop : true,
							keyboard : false,
							show : false
						});
				$("#bomEditDialog_saveBtn").unbind('click');
				$("#bomEditDialog_mtrlCatesel").unbind('change');
				$("#bomEditDialog_saveBtn").bind('click', this.saveBomMtrlFnc);
				$("#bomEditDialog_mtrlCatesel").bind('change',
                    this.bomEditDialogMtrlCateSelFnc);
				$('#bomEditDialog').modal("show");

				$("#bomEditDialogForm").attr(VAL.ENABLED_ATTR);
				$("#bomEditDialogForm select").attr(VAL.ENABLED_ATTR);
				domObj.dialogs.$bomEditDialog_mtrlUseQtyTxt
					.attr(VAL.ENABLED_ATTR);
				domObj.dialogs.$bomEditDialog_mdlIdTxt.val(domObj.$mdlIdTxt
						.val());
				domObj.dialogs.$bomEditDialog_mtrlUseQtyTxt.val("");
				
				inObj = {
					trx_id : VAL.T_FBPBISOPE,
					action_flg : 'L'
				};
				outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code == VAL.NORMAL) {
					domObj.dialogs.$bomEditDialog_OpeIDSel.empty();
					SelectDom.addSelectArr(
							domObj.dialogs.$bomEditDialog_OpeIDSel,
							outObj.oary, "ope_id");
				}
				comAddValueByDataCateFnc("#bomEditDialog_mtrlCatesel","MTCT","data_ext","data_desc",true);
				
				if(domObj.dialogs.$bomEditDialog_mtrlCatesel.val()!=""){
					inObj = {
						 trx_id : VAL.T_FBPBISMTR,
						 action_flg : 'Q',
						 iary : [{
							 mtrl_cate : domObj.dialogs.$bomEditDialog_mtrlCatesel.val()
						 }]
					 };
					 outObj = comTrxSubSendPostJson(inObj);
					 if (outObj.rtn_code == VAL.NORMAL) {
						 domObj.dialogs.$bomEditDialog_mtrlProdIDSel.empty();
					 SelectDom.addSelectArr(
						 domObj.dialogs.$bomEditDialog_mtrlProdIDSel,
						 outObj.oary, "mtrl_prod_id");
					 }
				}
				 

				if (actionFlg === "A") {
					
				} else {
					domObj.dialogs.$bomEditDialog_mtrlProdIDSel
							.attr(VAL.DISABLED_ATTR);
					domObj.dialogs.$bomEditDialog_mtrlMkrTxt
							.attr(VAL.DISABLED_ATTR);
					domObj.dialogs.$bomEditDialog_mtrlCatesel
							.attr(VAL.DISABLED_ATTR);
					
					domObj.dialogs.$bomEditDialog_mdlMtrlSeqIDTxt
						.val(bomRowData.mdl_mtrl_seq_id);
					SelectDom.setSelect(
							domObj.dialogs.$bomEditDialog_mtrlCatesel,
							bomRowData.mtrl_cate);
					SelectDom.setSelect(
							domObj.dialogs.$bomEditDialog_mtrlProdIDSel,
							bomRowData.mtrl_prod_id_fk);
					SelectDom.setSelect(domObj.dialogs.$bomEditDialog_OpeIDSel,
							bomRowData.ope_id_fk);
					domObj.dialogs.$bomEditDialog_mtrlUseQtyTxt
							.val(bomRowData.mtrl_use_qty);
					
				}
			},
			saveBomMtrlFnc : function() {
                var actionFlg, iary, inObj, outObj, newRowId, mdlId;

                actionFlg = domObj.dialogs.$bomEditDialog_mtrlProdIDSel
                    .attr("disabled") === "disabled" ? "U" : "A";
                mdlId = domObj.dialogs.$bomEditDialog_mdlIdTxt.val();
                var mtrlpro = domObj.dialogs.$bomEditDialog_mtrlProdIDSel.find("option:selected").text();
                var mtrlCate = domObj.dialogs.$bomEditDialog_mtrlCatesel.val();
                iary = {
                    mdl_id_fk: mdlId,
                    mtrl_prod_id_fk: domObj.dialogs.$bomEditDialog_mtrlProdIDSel
                        .find("option:selected").text(),
                    mdl_mtrl_seq_id: domObj.dialogs.$bomEditDialog_mdlMtrlSeqIDTxt.val(),
                    mtrl_cate: domObj.dialogs.$bomEditDialog_mtrlCatesel.find("option:selected").val(),
                    ope_id_fk: domObj.dialogs.$bomEditDialog_OpeIDSel.val(),
                    mtrl_unit_fk: "",
                    mtrl_use_qty: domObj.dialogs.$bomEditDialog_mtrlUseQtyTxt.val(),
                    cns_flg: "Y",
                    mnt_flg: "Y"
                };
                if (actionFlg == "A") {
                var ids = domObj.grids.$bomListGrd.jqGrid('getDataIDs');
                    for (var z = 0; z < ids.length; z++) {
                        var rowDatas = domObj.grids.$bomListGrd.jqGrid("getRowData", ids[z]);
                        if (rowDatas.mtrl_cate == mtrlCate) {
                            showErrorDialog("", "选择物料种类重复，请检查！");
                            return false;
                            if (rowDatas.mtrl_prod_id_fk == mtrlpro) {
                                showErrorDialog("", "选择原料产品代码重复，请检查！");
                                return false;
                            }
                        }
                    }
                        domObj.grids.$bomListGrd.jqGrid("addRowData", newRowId, iary);
                        domObj.dialogs.$bomEditDialog.modal("hide");
                        showSuccessDialog("添加物料成功,点击保存确认！");
                    } else if (actionFlg == "U") {
                        var selRowId;
                        selRowId = domObj.grids.$bomListGrd.jqGrid("getGridParam",
                            "selrow");
                        domObj.grids.$bomListGrd.jqGrid("setRowData", selRowId, iary);
                        domObj.dialogs.$bomEditDialog.modal("hide");
                        showSuccessDialog("修改物料成功,点击保存确认！");
                    }
            },

			deleteBomMtrlFnc : function() {
				if(domObj.$mdlDscTxt.attr("disabled") == "disabled"){
					showErrorDialog("", "目前不是编辑状态，不可删除Bom信息。");
					return false;
				}
				var selRowId, rowData, inObj, outObj;
				selRowId = domObj.grids.$bomListGrd.jqGrid("getGridParam", "selrow");
				if (!selRowId) {
					showErrorDialog("", "请选择需要删除的物料");
					return false;
				}
				domObj.buttons.$deleteBomMtrl.showCallBackWarnningDialog({
					errMsg : "是否删除物料信息,请确认!!!!",
					callbackFn : function(data) {
						if (data.result == true) {
//							rowData = domObj.grids.$bomListGrd.jqGrid(
//									"getRowData", selRowId);
//							inObj = {
//								trx_id : VAL.T_FBPBISBOM,
//								action_flg : "D",
//								iary : [{
//									mdl_id_fk : rowData.mdl_id_fk,
//									mtrl_prod_id_fk : rowData.mtrl_prod_id_fk,
//									mdl_mtrl_seq_id : rowData.mdl_mtrl_seq_id
//								}]
//							};
//							outObj = comTrxSubSendPostJson(inObj);
//							if (outObj.rtn_code === VAL.NORMAL) {
//								showSuccessDialog("删除物料成功");
								domObj.grids.$bomListGrd.jqGrid("delRowData", selRowId);
//							}
						}
					}
				});
			}
		}
	};


	var btnsFnc = {
		query_func : function() {
            if(domObj.$mdlDscTxt.attr("disabled") != "disabled"){
                showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
                return false;
            }
			domObj.dialogs.$queryMdlDefDialog.modal({
						backdrop : true,
						keyboard : false,
						show : false
					});
			domObj.dialogs.$queryMdlDefDialog_queryBtn.unbind('click');
			domObj.dialogs.$queryMdlDefDialog.modal("show");
			domObj.dialogs.$queryMdlDefDialog_queryBtn.bind('click',
					dialogFnc.queryDialog.queryFnc);
			domObj.dialogs.$queryMdlDefDialog_mdlIdTxt.attr(VAL.ENABLED_ATTR);
			domObj.dialogs.$queryMdlDefDialog_mdlIdTxt.val("");
			domObj.dialogs.$queryMdlDefDialog_mdlIdTxt.focus();
		},
		delete_func : function() {
			if(domObj.$mdlDscTxt.attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
			var selRowId, rowData, iary, inObj, outObj;
			selRowId = domObj.grids.$mdlDefListGrd.jqGrid("getGridParam",
					"selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要删除的产品信息");
				return false;
			}
			domObj.buttons.$delete_btn.showCallBackWarnningDialog({
						errMsg : "是否删除产品信息,请确认!!!!",
						callbackFn : function(data) {
							if (data.result == true) {
								rowData = domObj.grids.$mdlDefListGrd.jqGrid(
										"getRowData", selRowId);
								iary = {
									mdl_id : rowData.mdl_id
								};
								inObj = {
									trx_id : VAL.T_FBPBMDLDF,
									action_flg : "D",
									iary : [iary]
								};
								outObj = comTrxSubSendPostJson(inObj);
								if (outObj.rtn_code === VAL.NORMAL) {

									domObj.grids.$mdlDefListGrd.jqGrid(
											"delRowData", selRowId);
									controlsFunc.com_get_1st_inf();
                                    showSuccessDialog("删除产品信息成功");
                                    // setTimeout("dialog.modal('hide')",1000);
								}
							}
							
						}
					});
		},
		add_func : function() {
			if(domObj.$mdlIdTxt.attr("disabled") != "disabled"){
				showErrorDialog("", "已经在新增中，请勿重复新增");
				return false;
			}
			if(domObj.$mdlDscTxt.attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
            domObj.grids.$bomListGrd.jqGrid("clearGridData");
            domObj.grids.$levelListGrd.jqGrid("clearGridData");
            $("input[type='text']").val("");
            SelectDom.initWithSpace($("select"),"","");
			pageFnc.initData();
			$("#mdlDefConditionForm input").attr(VAL.ENABLED_ATTR);
			$("#mdlDefConditionForm select").attr(VAL.ENABLED_ATTR);
			//清空pathIDVer
			PathIdAndVer ={};
			domObj.$mdlIdTxt.focus();
		},
		update_func : function() {
			if(domObj.$mdlDscTxt.attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
			var path_id_fk, path_ver_fk, mdl_cate, cus_id, layout_id;
			var selRowId = domObj.grids.$mdlDefListGrd.jqGrid("getGridParam","selrow");
			path_id_fk = domObj.$pathIdSel.val();
			path_ver_fk = domObj.$pathVerSel.val();
			mdl_cate = domObj.$mdlCateSel.val();
			cus_id = domObj.$cusIDSel.val();
			layout_id = domObj.$layoutIDSel.val();
			if (!selRowId) {
				showErrorDialog("", "请选择需要修改的产品信息");
				return false;
			}
			$("#mdlDefConditionForm input:not(#mdlIdTxt)")
					.attr(VAL.ENABLED_ATTR);
			$("#mdlDefConditionForm select").attr(VAL.ENABLED_ATTR);
			domObj.$mdlDscTxt.focus();
		},
		copy_func:function(){
			if(domObj.$mdlDscTxt.attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
			var selRowId = domObj.grids.$mdlDefListGrd.jqGrid("getGridParam","selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要复制的产品信息");
				return false;
			}
			$("#mdlDefConditionForm input").attr(VAL.ENABLED_ATTR);
			$("#mdlDefConditionForm select").attr(VAL.ENABLED_ATTR);
			domObj.$mdlIdTxt.focus();
		},
		rollback_func : function(){
			if(domObj.$mdlDscTxt.attr("disabled") == "disabled"){
				showErrorDialog("", "没有编辑，无需撤回");
				return false;
			}
			$("#mdlDefConditionForm input").attr(VAL.DISABLED_ATTR);
			$("#mdlDefConditionForm select").attr(VAL.DISABLED_ATTR);
			var outObj = comTrxSubSendPostJson(inObj_Query);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#mdlDefListGrd");
				domObj.dialogs.$queryMdlDefDialog.modal("hide");
				controlsFunc.com_get_1st_inf();
			}
		},
		save_func : function() {
			var actionFlg, inObj, outObj, iary, iaryB= new Array();
			var iaryC = [];
			var mdlId, mdlDsc, layoutId, mdlCate, mdlSize,okRatio,cus_id,cus_mdl_id,path_id,path_ver;
			var fromThickness, toThickness, boxCnt, swh_time, max_pf_cnt, valid_flg,post_flag,max_rwk_cnt;
			actionFlg = domObj.$mdlIdTxt.attr("disabled") === "disabled"
					? "U"
					: "A";
			mdlId = $.trim(domObj.$mdlIdTxt.val());
			mdlDsc = $.trim(domObj.$mdlDscTxt.val());
			mdlCate = $.trim(domObj.$mdlCateSel.val());
			mdlSize = $.trim(domObj.$mdlSizeTxt.val());
			path_id = domObj.$pathIdSel.val()==null ? "" : $.trim(domObj.$pathIdSel.val());
			swh_time = $.trim(domObj.$SWHtimeTxt.val());
			max_pf_cnt = $.trim(domObj.$maxPFCntTxt.val());
			valid_flg = domObj.$validFlgSel.val();
            post_flag = domObj.$lotFlgSel.val();
            max_rwk_cnt = domObj.$maxRwCntTxt.val();
			var rowIds = domObj.grids.$bomListGrd.jqGrid('getDataIDs');
			for(var i=0;i<rowIds.length;i++){
				var rowData = domObj.grids.$bomListGrd.jqGrid('getRowData', rowIds[i]);
				var bom_info ={
					mdl_id_fk : mdlId,
					mtrl_prod_id_fk : $.trim(rowData.mtrl_prod_id_fk),
					mdl_mtrl_seq_id : rowData.mdl_mtrl_seq_id,
					mtrl_cate : rowData.mtrl_cate,
					ope_id_fk : rowData.ope_id_fk,
					mtrl_unit_fk : "",
					mtrl_use_qty : rowData.mtrl_use_qty,
					cns_flg : "Y",
					mnt_flg : "Y"
				}
				iaryB.push(bom_info);
			}
			
			var rowIds2 = domObj.grids.$levelListGrd.jqGrid('getDataIDs');
			for(var i = 0 ;i < rowIds2.length;i++){
				var rowData = domObj.grids.$levelListGrd.jqGrid('getRowData', rowIds2[i]);
				var level_info = {
						evt_seq_id : rowData.evt_seq_id,
						mdl_id_fk : mdlId,
						low_ng_limt : rowData.low_ng_limt,
						up_ng_limt : rowData.up_ng_limt,
						prd_grade : rowData.prd_grade,
				}
				iaryC.push(level_info);
			}
			
			
			if (!mdlId) {
				showErrorDialog("", "产品代码不能为空");
				return false;
			}
			if (!mdlDsc) {
				showErrorDialog("", "产品描述不能为空");
				return false;
			}
			if(!mdlCate){
				showErrorDialog("","请选择产品种类");
				return false;
			}
//			if(!mdlSize){
//				showErrorDialog("","产品尺寸不能为空");
//				return false;
//			}
            if(!max_pf_cnt){
                showErrorDialog("","请输入外箱最大装包数量");
                return false;
            }else if(!ComRegex.isInt(max_pf_cnt)){
                showErrorDialog("","外箱最大装包数量不是有效的整数");
                return false;
            }
            
			if(!path_id){
				showErrorDialog("","请选择产品工艺路线");
				return false;
			}
            
            if(!max_rwk_cnt){
                showErrorDialog("","请输入内包最大装片数量");
                return false;
            }else if(!ComRegex.isInt(max_rwk_cnt)){
                showErrorDialog("","内包最大装片数量不是有效的整数");
                return false;
            }
			iary = [{
				mdl_id : mdlId,
				mdl_dsc : mdlDsc,
				layot_id_fk : layoutId,
				mdl_cate : mdlCate,
				mdl_size : mdlSize,
				path_id_fk : path_id,
				path_ver_fk : '00000',
				from_thickness : fromThickness,
				to_thickness : toThickness,
				max_pf_cnt : max_pf_cnt,
				swh_time   : swh_time,
				valid_flg : valid_flg,
                post_flag : post_flag,
                max_rwk_cnt: max_rwk_cnt,
				iaryB : iaryB,
				iaryC : iaryC
			}];
			if (boxCnt) {
				iary.box_cnt = boxCnt;
			}
			if (okRatio) {
				iary.ok_ratio = okRatio;
			}
			if (toThickness) {
				iary.to_thickness = toThickness;
			}
			inObj = {
				trx_id : VAL.T_FBPBMDLDF,
				action_flg : actionFlg,
				evt_usr : VAL.EVT_USR = $("#userId").text(),
				iary : iary
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				
				$("input").attr(VAL.DISABLED_ATTR);
				$("select").attr(VAL.DISABLED_ATTR);
				
				if (actionFlg == "A") {
					newRowId = getGridNewRowID("#mdlDefListGrd");
					domObj.grids.$mdlDefListGrd.jqGrid("addRowData", newRowId,
							iary);
					domObj.grids.$mdlDefListGrd.setSelection(newRowId, true); 
					showSuccessDialog("新增产品信息成功");
					
				} else if (actionFlg == "U") {
					var rowid = domObj.grids.$mdlDefListGrd.jqGrid('getGridParam','selrow');
					controlsFunc.mdlGridSelRowFnc(rowid);
					showSuccessDialog("产品信息更新成功");
				}
			}

		},

		clear_func : function() {
            domObj.grids.$bomListGrd.jqGrid("clearGridData");
            domObj.grids.$mdlDefListGrd.jqGrid("clearGridData");
            domObj.grids.$levelListGrd.jqGrid("clearGridData");
			$("input[type='text']").val("");
			SelectDom.setSelect($("select"),"","");
            $("input").attr(VAL.DISABLED_ATTR);
            $("select").attr(VAL.DISABLED_ATTR);
            //清空pathIDVer
            PathIdAndVer ={};
		}

	};
	var pageFnc = {
		initControl : function() {
			controlsFunc.initMdlGirdInfo();
			controlsFunc.initBomGridInfo();
			controlsFunc.initlevelGridInfo();
			$("#mdlDefConditionForm input").attr(VAL.DISABLED_ATTR);
			$("#mdlDefConditionForm select").attr(VAL.DISABLED_ATTR);
		},
		initData : function() {
			controlsFunc.initMdlCateSel();
			controlsFunc.initPathIdSel();
			controlsFunc.initValidFlag();
            controlsFunc.initPostFlag();
			controlsFunc.initCusID();
		},
		
		clear : function() {
			domObj.grids.$bomListGrd.jqGrid("clearGridData");
			domObj.grids.$mdlDefListGrd.jqGrid("clearGridData");
			domObj.grids.$levelListGrd.jqGrid("clearGridData");
			$("input[type='text']").val("");
            SelectDom.initWithSpace($("select"),"","");
		},
		initEvent : function() {
			domObj.buttons.$query_btn.click(btnsFnc.query_func);
			domObj.buttons.$delete_btn.click(btnsFnc.delete_func);
			domObj.buttons.$update_btn.click(btnsFnc.update_func);
			domObj.buttons.$add_btn.click(btnsFnc.add_func);
			domObj.buttons.$save_btn.click(btnsFnc.save_func);
			domObj.buttons.$clear_btn.click(btnsFnc.clear_func);
			domObj.buttons.$rollback_btn.click(btnsFnc.rollback_func);
			domObj.buttons.$copy_btn.click(btnsFnc.copy_func);
			domObj.buttons.$addBomMtrl.click(dialogFnc.bomDialog.addBom);
			domObj.buttons.$updateBomMtrl.click(dialogFnc.bomDialog.updateBom);
			domObj.buttons.$deleteBomMtrl
					.click(dialogFnc.bomDialog.deleteBomMtrlFnc);
			
			domObj.buttons.$add_level_btn.click(dialogFnc.levelDialog.leveladdFnc);
			domObj.buttons.$update_level_btn.click(dialogFnc.levelDialog.levelupdateFnc);
			domObj.buttons.$del_level_btn.click(dialogFnc.levelDialog.leveldeleteFnc);
		}
	};
	pageFnc.clear();
	pageFnc.initData();
	pageFnc.initControl();
	pageFnc.initEvent();
	
	domObj.$pathIdSel.change(function(){
		//check有无待产工单  addbyxxd
//        controlsFunc.checkExistWoId();

		controlsFunc.pathIdChangeFunc();
	})
	function button_disabled(disabled_attr,fadeTo){
		 domObj.buttons.$rollback_btn.fadeTo("fast",0.25);
		 domObj.buttons.$toolPt_addBtn.fadeTo("fast",0.25);
		 domObj.buttons.$toolPt_updateBtn.fadeTo("fast",0.25);
		 domObj.buttons.$toolPt_delBtn.fadeTo("fast",0.25);
		 domObj.buttons.$add_btn.attr(VAL.DISABLED_ATTR);
		 
		 domObj.buttons.$copy_btn.attr(VAL.DISABLED_ATTR);
		 domObj.buttons.$delete_btn.attr(VAL.DISABLED_ATTR);
		 
	  }


    /**
     * All controls's jquery object/text
     * @type {Object}
     */
    var domObjs = {
        W                   : $(window)       ,
        grids : {
            $leftDiv         : $(".leftDiv"),
            $rightDiv		: $(".rightDiv"),
            $mdlDefListDiv : $("#mdlDefListDiv"),
            $mdlDefListGrd : $("#mdlDefListGrd")
        }
    };


    //jqGrid表格自适应
    function resizeFnc(){
        var grdDivHeight,grdDivWidth,offsetBottom1, grdDivWidth1,offsetBottom2, grdDivWidth2;
        grdDivWidth = domObjs.W.width() - 120 - domObjs.grids.$rightDiv.width();
        grdDivHeight = domObjs.W.height() - domObjs.grids.$leftDiv.offset().top;

        domObjs.grids.$leftDiv.height(grdDivHeight);
        domObjs.grids.$mdlDefListDiv.width(grdDivWidth);
        domObjs.grids.$mdlDefListDiv.height(grdDivHeight*0.99 - 51);
        domObjs.grids.$mdlDefListGrd.setGridWidth(grdDivWidth*0.99);
        domObjs.grids.$mdlDefListGrd.setGridHeight(grdDivHeight*0.95 - 101);

        //2
        grdDivWidth1 = domObjs.W.width() - 500 - domObjs.grids.$leftDiv.width();
        offsetBottom1 = $(window).height() - $(".rightDiv").offset().top - 100;

        $("#levelListDiv").width(grdDivWidth1*0.98);
        $("#levelListDiv").height(offsetBottom1*0.99 - 75);
        $("#levelListGrd").setGridWidth(grdDivWidth1*0.97);
        $("#levelListGrd").setGridHeight(offsetBottom1*0.95-51 - 75);
        //3
        grdDivWidth2 = domObjs.W.width() - 500 - domObjs.grids.$leftDiv.width();
        offsetBottom2 = $(window).height() - $(".rightDiv").offset().top - 100;

        $("#bomListDiv").width(grdDivWidth2*0.98);
        $("#bomListDiv").height(offsetBottom2*0.99 - 75);
        $("#bomListGrd").setGridWidth(grdDivWidth2*0.97);
        $("#bomListGrd").setGridHeight(offsetBottom2*0.95-51 - 75);
    };

    resizeFnc();
    domObjs.W.resize(function() {
        resizeFnc();
    });
})